#include "internal.h"

static inline u64 cfs_rq_min_slice(struct cfs_rq *cfs_rq)
{
    struct sched_entity *root = __pick_root_entity(cfs_rq);
    struct sched_entity *curr = cfs_rq->curr;
    u64 min_slice = ~0ULL;

    if (curr && curr->on_rq)
        min_slice = curr->slice;

    if (root)
        min_slice = min(min_slice, root->min_slice);

    return min_slice;
}

static inline bool protect_slice(struct sched_entity *se)
{
    return ((s64)(se->vprot - se->vruntime) > 0);
}

/*
 * Set the vruntime up to which an entity can run before looking
 * for another entity to pick.
 * In case of run to parity, we use the shortest slice of the enqueued
 * entities to set the protected period.
 * When run to parity is disabled, we give a minimum quantum to the running
 * entity to ensure progress.
 */
static inline void set_protect_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
{
    u64 slice = normalized_sysctl_sched_base_slice;
    u64 vprot = se->deadline;

    if (sched_feat(RUN_TO_PARITY))
        slice = cfs_rq_min_slice(cfs_rq);

    slice = min(slice, se->slice);
    if (slice != se->slice)
        vprot = min_vruntime(vprot, se->vruntime + calc_delta_fair(slice, se));

    se->vprot = vprot;
}
